package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketChannel;
import com.cskaoyan.market.db.domain.MarketOrder;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.AdminOrderService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/order")
public class AdminOrderController extends HttpServlet {

    @Autowired
    AdminOrderService adminOrderService;
    @Autowired
    private SecurityManager securityManager;



    @GetMapping("channel")
    public Object channel()  {
        MarketChannel[] orderList = adminOrderService.channel();
        //对于分页查询的这些接口，那么我们需要调用oKlIST方法
        Object o = ResponseUtil.ok(orderList);
        return o;
    }

    @GetMapping("detail")
    @RequiresPermissions("admin:order:detail")
    @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"},button = "详情")
    public Object detail(Integer id)  {
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketAdmin admin = (MarketAdmin) session.getAttribute("user");
        String name=admin.getUsername();
        String avatar=admin.getAvatar();
        Map<String,Object> data = adminOrderService.detail(id);
        MarketUser user = new MarketUser();
        user.setNickname(name);
        user.setAvatar(avatar);
        data.put("user",user);
        //对于分页查询的这些接口，那么我们需要调用oKlIST方法
        Object o = ResponseUtil.ok(data);
        return o;
    }

    @GetMapping("list")
    @RequiresPermissions("admin:order:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"},button = "查询")
    public Object list(Integer page,Integer limit,String[] orderStatusArray,String sort,String order,String start, String end,String orderSn,Integer userId) {
        List<MarketOrder> orderList = adminOrderService.list(page, limit,orderStatusArray, sort, order,start,end,orderSn,userId);
        //对于分页查询的这些接口，那么我们需要调用oKlIST方法
        Object o = ResponseUtil.okList(orderList);
        return o;
    }



    @PostMapping("delete")
    @RequiresPermissions("admin:order:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"},button = "删除")
    public Object delete(@RequestBody Map map)  {
        Integer orderId = (Integer) map.get("orderId");
        int i=adminOrderService.delete(orderId);
        Object o ;
        if(i==623){
            o= ResponseUtil.fail(623,"订单不能删除");
        }else{
            o= ResponseUtil.ok(i);
        }
       return o;
    }


    @PostMapping("ship")
    @RequiresPermissions("admin:order:ship")
    @RequiresPermissionsDesc(menu = {"商场管理", "发货管理"},button = "发货")
    public Object ship(@RequestBody Map map)  {

        Integer orderId = (Integer) map.get("orderId");
        String shipChannel= (String) map.get("shipChannel");
        String shipSn= (String) map.get("shipSn");

        int i=adminOrderService.ship(orderId,shipChannel,shipSn);
        Object o = ResponseUtil.ok(i);
        return o;
    }

    @PostMapping("refund")
    @RequiresPermissions("admin:order:refund")
    @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"},button = "退款")
    public Object refund(@RequestBody Map map) {

        Integer orderId = (Integer) map.get("orderId");
        Integer refundMoney = (Integer) map.get("refundMoney");


        int i=adminOrderService.refund(orderId,refundMoney);
        Object o = ResponseUtil.ok(i);
        return o;
    }
}
